using System.Collections.Generic;
using System.Linq;
using TaxiService.Application;
using TaxiService.CommandHandlers;
using TaxiService.Commands;

namespace TaxiService.CommandBus
{
  public class CommandBus : ICommandBus
  {
    private readonly IDiContainer _container;

    public CommandBus(IDiContainer container)
    {
      _container = container;
    }

    public void Publish<T>(T command) where T : ICommand
    {
      _container.ResolveAll<ICommandHandler<T>>()
        .ToList().ForEach(x => x.Execute(command));
    }

    public void Publish<T>(IEnumerable<T> messages) where T : ICommand
    {
      foreach (var message in messages)
      {
        Publish(message);
      }
    }
  }
}